<?php
namespace Home\Model;
use Think\Model;
class OrdersModel extends Model{
	public function order_list($pageNo,$pageSize,$where)
	{    
		 $order=M('order');
		 
		
		 $where['status']='1';
		 $where['sales_return'] = '';
		 $order_list=$order->where($where)->limit(($pageNo-1)*$pageSize,$pageSize)->select();
		 $sum=$order->where($where)->select();
		  
		 $count['count']=$order->where($where)->count();
		 $count['countpage']=ceil($count['count']/$pageSize);
		 if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		 }
		 if($pageNo<=0){
		 	$pageNo=1;
		 }
		 
		 $count['sum']=count($sum);
		 $count['pageSize']=$pageSize;
		 $count['pageNo']=$pageNo;
		 $count['num']=$count['countpage']-4;
		
		 $result['order_list']=$order_list;
		 $result['count']=$count;
		 
		 
		 return $result;
		 
	}
	
	
	public function wait_list($pageNo,$pageSize,$wheres)
	{    
		
		 
		 
		 $where='1=1';
		 $where.=" and status = 2  ";
		 
		//业务员权限
	    if($wheres['type']=='2')
		{
			$where.=' and salesman = "'.$_SESSION['user'].'"';	
		}
		 //模糊查询订单号
	    if(!empty($wheres['order_number']))
		{
			$where.=' and order_number like "%'.$wheres['order_number'].'%"';
		}
		
		 //模糊查询客户
	    if(!empty($wheres['customer_name']))
		{
			$where.=' and customer_name like "%'.$wheres['customer_name'].'%"';
		}
		
		//模糊查询业务员
	    if(!empty($wheres['salesman']))
		{
			$where.=' and salesman like "%'.$wheres['salesman'].'%"';
			
		}
		 $order=M('order');
		 $order_list=$order->where($where)->limit(($pageNo-1)*$pageSize,$pageSize)->select();
		 
		 $order_count=$order->where($where)->select();	
		 $count['count']=count($order_count);
		 $count['countpage']=ceil($count['count']/$pageSize);
		 
		 
		 if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		 }
		 if($pageNo<=0){
		 	$pageNo=1;
		 }
		 
		 $count['pageSize']=$pageSize;
		 $count['pageNo']=$pageNo;
		 $count['num']=$count['countpage']-4;
		
		 $result['order_list']=$order_list;
		 $result['count']=$count;
		 return $result;
		 
	}
	
	
	/*public function wait_list_own($pageNo,$pageSize,$wheres,$own)
	{    
	 	 if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		 }
		 if($pageNo<=0){
		 	$pageNo=1;
		 }
		 $where='1=1';
		 $where.=" and status = 2";
		 //模糊查询订单号
	    if(!empty($wheres['order_number']))
		{
			$where.=' and order_number like "%'.$wheres['order_number'].'%"';
		}
		
		 //模糊查询客户
	    if(!empty($wheres['customer_name']))
		{
			$where.=' and customer_name like "%'.$wheres['customer_name'].'%"';
		}
		
		 $where['salesman'] = $own;
		 $order=M('order');
		 
		 $order_list=$order->where($where)->limit(($pageNo-1)*$pageSize,$pageSize)->select();
		
		 
		 $count['count']=count($order_list);
		 $count['countpage']=ceil($count['count']/$pageSize);
		 
		
		 
		 $count['pageSize']=$pageSize;
		 $count['pageNo']=$pageNo;
		 $count['num']=$count['countpage']-4;
		
		 $result['order_list']=$order_list;
		 $result['count']=$count;
		 return $result;
		 
	}*/
	
	public function complete_list($pageNo,$pageSize,$wheres)
	{    
		 
		 
		 $where='1=1';
		$where.=" and status = 4";
		
		//业务员权限
	    if($wheres['type']=='2')
		{
			$where.=' and salesman = "'.$_SESSION['user'].'"';	
		}
		
		 //模糊查询订单号
	    if(!empty($wheres['order_number']))
		{
			$where.=' and order_number like "%'.$wheres['order_number'].'%"';
		}
		
		//模糊查询订单号
	    if(!empty($wheres['salesman']) && $wheres['type']=='1')
		{
			$where.=' and salesman like "%'.$wheres['salesman'].'%"';
		}
		
		//模糊查询订单号
	    if(!empty($wheres['customer_name']))
		{
			$where.=' and customer_name like "%'.$wheres['customer_name'].'%"';
		}
		
		 $order=M('order');
		 
		 $complete_list=$order->where($where)->limit(($pageNo-1)*$pageSize,$pageSize)->select();
		 
		 $complete_count=$order->where($where)->select();
		 
		 $count['count']=count($complete_count);
		 $count['countpage']=ceil($count['count']/$pageSize);
		 
		 if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		 }
		 if($pageNo<=0){
		 	$pageNo=1;
		 }
		 
		 $count['pageSize']=$pageSize;
		 $count['pageNo']=$pageNo;
		 $count['num']=$count['countpage']-4;
		
		 $result['complete_list']=$complete_list;
		 $result['count']=$count;
		 return $result;
		 
	}
	
	/*public function complete_list_own($pageNo,$pageSize,$wheres,$own)
	{    
		 $where['status'] = $wheres;
		 $where['salesman'] = $own;
		 $order=M('order');
		 
		 $complete_list=$order->where($where)->limit(($pageNo-1)*$pageSize,$pageSize)->select();
		 
		 $count['count']=count($order_list);
		 $count['countpage']=ceil($count['count']/$pageSize);
		 
		 if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		 }
		 if($pageNo<=0){
		 	$pageNo=1;
		 }
		 
		 $count['pageSize']=$pageSize;
		 $count['pageNo']=$pageNo;
		 $count['num']=$count['countpage']-4;
		
		 $result['complete_list']=$complete_list;
		 $result['count']=$count;
		 return $result;
		 
	}*/
	
	public function delivery_list($pageNo,$pageSize,$wheres)
	{    
	 	
		
		$where='1=1';
		$where.=" and status = 3 and sales_return = ''";
		
		//业务员权限
	    if($wheres['type']=='2')
		{
			$where.=' and salesman = "'.$_SESSION['user'].'"';	
		}
		
		 //模糊查询订单号
	    if(!empty($wheres['order_number']))
		{
			$where.=' and order_number like "%'.$wheres['order_number'].'%"';
		}
		
		//模糊查询订单号
	    if(!empty($wheres['salesman']) && $wheres['type']=='1')
		{
			$where.=' and salesman like "%'.$wheres['salesman'].'%"';
		}
		
		//模糊查询订单号
	    if(!empty($wheres['customer_name']))
		{
			$where.=' and customer_name like "%'.$wheres['customer_name'].'%"';
		}
		 $order=M('order');
		 
		 $order_list=$order->where($where)->limit(($pageNo-1)*$pageSize,$pageSize)->select();
		 $order_count=$order->where($where)->select();
		 $count['count']=count($order_count);
		 $count['countpage']=ceil($count['count']/$pageSize);
		 
		 
		 
		if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		}
		if($pageNo<=0){
			$pageNo=1;
		}
		 
		 $count['pageSize']=$pageSize;
		 $count['pageNo']=$pageNo;
		 $count['num']=$count['countpage']-4;
		
		 $result['order_list']=$order_list;
		 $result['count']=$count;
		 return $result;
		 
	}
	
	
	/*public function delivery_list_own($pageNo,$pageSize,$wheres,$own)
	{    
		 $where['status'] = $wheres;
		 $where['salesman'] = $own;
		 $order=M('order');
		 
		 $order_list=$order->where($where)->limit(($pageNo-1)*$pageSize,$pageSize)->select();
		 
		 $count['count']=count($order_list);
		 $count['countpage']=ceil($count['count']/$pageSize);
		 
		 if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		 }
		 if($pageNo<=0){
		 	$pageNo=1;
		 }
		 
		 $count['pageSize']=$pageSize;
		 $count['pageNo']=$pageNo;
		 $count['num']=$count['countpage']-4;
		
		 $result['order_list']=$order_list;
		 $result['count']=$count;
		 return $result;
		 
	}*/
    
	public function details_list($wheres){
		$product = M("product");
		$where['sku'] = $wheres;
		if(!empty($where['sku'])){
			$result = $product->where($where)->find();
		return $result;
		}
		
	}
	
	//查询订单表内是否存在该订单号
	public function order_number($number){
		$order = M("order");
		$where["order_number"] = $number;
		$result = $order->where($where)->find();
		return $result;
	}
	

	//查询子订单表内是否存在该订单号
	public function order_number1($number){
		$order = M("order_info");
		$where["order_number"] = $number;
		$result = $order->where($where)->find();
		return $result;
	}

	public function product_info($wheres){
		$product = M("product");
		$where['sku'] = $wheres;
		$res = $product->field("sku,product_material,product_type")->where($where)->find();
		return $res;
	}
	
	
	public function add_remarks($id,$remarks){
		$where['id'] = $id;
		$data['remarks'] = $remarks;
		$order = M("order");
		$res = $order->where($where)->save($data);
		return $res;
		
	}
	
	//添加子订单表数据
	public function add_info($data,$sku,$quantity,$p_id,$supplier,$buyer,$main_pic,$cost_price){
		$order = M("order_info");
		$data['sku'] = $sku;
		$data['quantity'] = $quantity;
		$data['p_id'] = $p_id;
		$data['supplier'] = $supplier;
		$data['buyer'] = $buyer;
		$data['main_pic'] = $main_pic;
		$data['cost_price'] = $cost_price;
		$data['status'] = '1';
		$order_number = $data['order_number'];
		$product_type = M('product')->field('product_type')->where(array('sku'=>$data['sku']))->find();
		$price_control = M('price_control')->where(array('type'=>$product_type['product_type']))->find();
		$Y1 =1;
		$x = 2;
		$check = M('order_info')->where(array('sku'=>$data['sku'],'order_number'=>$data['order_number']))->find();
		if($check==NULL){
			$quantity = $quantity;;
		}else{
			$quantity = $quantity+$check['quantity'];
		}
		if($quantity<=50){
			$x = $cost_price;
			$str1 = str_replace('x','$x',$price_control['ali_lt']);
			$str2 = str_replace('y1','$y1',$str1);
			$data['purchase_price']= eval(" return $str2;");
		}else{
			$x = $cost_price;
			$str3 = str_replace('x','$x',$price_control['ali_gt']);
			$str4 = str_replace('y2','$y2',$str3);
			$data['purchase_price']= eval(" return $str4;");
		} 
		$data['total_price']=$data['purchase_price']*$data['quantity'];
		$data['total_cost']=$data['cost_price']*$data['quantity'];
		//检测是否存在相同的SKU
		if($check==NULL){
			$res = $order->add(array('p_id'=>$data['p_id'],'order_number'=>$data['order_number'],'customer_name'=>$data['customer_name'],'salesman'=>$data['salesman'],'supplier'=>$data['supplier'],'buyer'=>$data['buyer'],'order_date'=>$time,'sku'=>$data['sku'],'quantity'=>$data['quantity'],'discount_rate'=>$data['discount_rate'],'main_pic'=>'B.jpg','cost_price'=>$data['cost_price'],'purchase_price'=>$data['purchase_price'],'total_price'=>$data['total_price'],'total_cost'=>$data['total_cost'],'status'=>'1'));
			
		}else{
			$data['quantity'] = $data['quantity']+$check['quantity'];
			$res = $order->where(array('sku'=>$data['sku'],'order_number'=>$check['order_number']))->save(array('p_id'=>$data['p_id'],'order_number'=>$data['order_number'],'customer_name'=>$data['customer_name'],'salesman'=>$data['salesman'],'supplier'=>$data['supplier'],'buyer'=>$data['buyer'],'order_data'=>$time,'sku'=>$data['sku'],'quantity'=>$data['quantity'],'discount_rate'=>$data['discount_rate'],'main_pic'=>'B.jpg','cost_price'=>$data['cost_price'],'purchase_price'=>$data['purchase_price'],'total_price'=>$data['total_price'],'total_cost'=>$data['total_cost'],'status'=>'1'));
		}
		
		//$res = $order->add(array('p_id'=>$data['p_id'],'order_number'=>$data['order_number'],'customer_name'=>$data['customer_name'],'salesman'=>$data['salesman'],'supplier'=>$data['supplier'],'buyer'=>$data['buyer'],'order_data'=>$time,'sku'=>$data['sku'],'quantity'=>$data['quantity'],'discount_rate'=>$data['discount_rate'],'main_pic'=>'B.jpg','cost_price'=>$data['cost_price'],'purchase_price'=>$data['purchase_price'],'total_price'=>$data['total_price'],'total_cost'=>$data['total_cost'],'status'=>'1'));
		return $res;
	}
	
	
	//新建订单草稿箱内没有该SKU添加子订单表数据
	public function edit_add($data,$sku,$quantity,$p_id,$supplier,$buyer,$main_pic,$cost_price){
		$order = M("order_info");
		$data['sku'] = $sku;
		$data['quantity'] = $quantity;
		$data['p_id'] = $p_id;
		$data['supplier'] = $supplier;
		$data['buyer'] = $buyer;
		$data['main_pic'] = $main_pic;
		$data['cost_price'] = $cost_price;
		$data['status'] = '1';
		$order_number = $data['order_number'];
		$product_type = M('product')->field('product_type')->where(array('sku'=>$data['sku']))->find();
		$price_control = M('price_control')->where(array('type'=>$product_type['product_type']))->find();
		$Y1 =1;
		$x = 2;
		if($quantity<=50){
			$x = $cost_price;
			$str1 = str_replace('x','$x',$price_control['ali_lt']);
			$str2 = str_replace('y1','$y1',$str1);
			$data['purchase_price']= eval(" return $str2;");
		}else{
			$x = $cost_price;
			$str3 = str_replace('x','$x',$price_control['ali_gt']);
			$str4 = str_replace('y2','$y2',$str3);
			$data['purchase_price']= eval(" return $str4;");
		} 
		$data['total_price']=$data['purchase_price']*$data['quantity'];
		$data['total_cost']=$data['cost_price']*$data['quantity'];
		$name = $_SESSION['user'];
		$time = date('Y-m-d H:i:s',time());
		$res = $order->add(array('p_id'=>$data['p_id'],'order_number'=>$data['order_number'],'customer_name'=>$data['customer_name'],'salesman'=>$name,'supplier'=>$data['supplier'],'buyer'=>$data['buyer'],'order_data'=>$time,'sku'=>$data['sku'],'quantity'=>$data['quantity'],'discount_rate'=>$data['discount_rate'],'main_pic'=>'A.jpg','cost_price'=>$data['cost_price'],'total_price'=>$data['total_price'],'total_cost'=>$data['total_cost'],'status'=>'1'));
		return $res;
	}
	
	//新建订单草稿箱内有该SKU修改子订单表数据
	public function edit_info($data,$sku,$quantity,$p_id,$supplier,$buyer,$main_pic,$cost_price){
		$order = M("order_info");
		$data['sku'] = $sku;
		$data['quantity'] = $quantity;
		$data['p_id'] = $p_id;
		$data['supplier'] = $supplier;
		$data['buyer'] = $buyer;
		$data['main_pic'] = $main_pic;
		$data['cost_price'] = $cost_price;
		$data['status'] = '1';
		$order_number = $data['order_number'];
		$product_type = M('product')->field('product_type')->where(array('sku'=>$data['sku']))->find();
		$price_control = M('price_control')->where(array('type'=>$product_type['product_type']))->find();
		$Y1 =1;
		$x = 2;
		if($quantity<=50){
			$x = $cost_price;
			$str1 = str_replace('x','$x',$price_control['ali_lt']);
			$str2 = str_replace('y1','$y1',$str1);
			$data['purchase_price']= eval(" return $str2;");
		}else{
			$x = $cost_price;
			$str3 = str_replace('x','$x',$price_control['ali_gt']);
			$str4 = str_replace('y2','$y2',$str3);
			$data['purchase_price']= eval(" return $str4;");
		} 
		$data['total_price']=$data['purchase_price']*$data['quantity'];
		$data['total_cost']=$data['cost_price']*$data['quantity'];
		//如果已存在SKU，修改价格和数量
		$res = $order->where(array('order_number'=>$data['order_number'],'sku'=>$sku,'status'=>'1','is_create'=>''))->save($data);
		
		return $res;
	}
	
	
	//待采购订单编辑没有该SKU添加子订单表数据
	public function edit_wait($data,$sku,$quantity,$p_id,$supplier,$buyer,$main_pic,$cost_price){
		$order = M("order_info");
		$data['sku'] = $sku;
		$data['quantity'] = $quantity;
		$data['p_id'] = $p_id;
		$data['supplier'] = $supplier;
		$data['buyer'] = $buyer;
		$data['main_pic'] = $main_pic;
		$data['cost_price'] = $cost_price;
		$data['status'] = '2';
		$order_number = $data['order_number'];
		$product_type = M('product')->field('product_type')->where(array('sku'=>$data['sku']))->find();
		$price_control = M('price_control')->where(array('type'=>$product_type['product_type']))->find();
		$Y1 =1;
		$x = 2;
		//echo $quantity;
		if($quantity<=50){
			$x = $cost_price;
			$str1 = str_replace('x','$x',$price_control['ali_lt']);
			$str2 = str_replace('y1','$y1',$str1);
			//$str2 = str_replace('y1','$y1',$str2);
			$data['purchase_price']= eval(" return $str2;");
		}else{
			$x = $cost_price;
			$str3 = str_replace('x','$x',$price_control['ali_gt']);
			$str4 = str_replace('y2','$y2',$str3);
			//$str4 = str_replace('y2','$y2',$str4);
			$data['purchase_price']= eval(" return $str4;");
		} 
		$data['total_price']=$data['purchase_price']*$data['quantity'];
		$data['total_cost']=$data['cost_price']*$data['quantity'];
		$data['alsobuy'] = $data['quantity'];
		$res = $order->add($data);
		return $res;
	}
	
	//待采购订单编辑有该SKU修改子订单表数据
	public function wait_info($data,$sku,$quantity,$p_id,$supplier,$buyer,$main_pic,$cost_price){
		$order = M("order_info");
		$data['sku'] = $sku;
		$data['quantity'] = $quantity;
		$data['p_id'] = $p_id;
		$data['supplier'] = $supplier;
		$data['buyer'] = $buyer;
		$data['main_pic'] = $main_pic;
		$data['cost_price'] = $cost_price;
		$data['status'] = '2';
		$order_number = $data['order_number'];
		$product_type = M('product')->field('product_type')->where(array('sku'=>$data['sku']))->find();
		$price_control = M('price_control')->where(array('type'=>$product_type['product_type']))->find();
		$Y1 =1;
		$x = 2;
		$wheres = " order_number ='".$order_number."' and sku='".$sku."' and (status='2' or status='3')";
		
		// $result = M("order_info")->where(array('order_number'=>$order_number,'sku'=>$sku,'status'=>'2'))->find();
		$result = M("order_info")->where($wheres)->find();
		if($quantity<=50){
			$x = $cost_price;
			$str1 = str_replace('x','$x',$price_control['ali_lt']);
			$str2 = str_replace('y1','$y1',$str1);
			//$str2 = str_replace('y1','$y1',$str2);
			$data['purchase_price']= eval(" return $str2;");
		}else{
			$x = $cost_price;
			$str3 = str_replace('x','$x',$price_control['ali_gt']);
			$str4 = str_replace('y2','$y2',$str3);
			//$str4 = str_replace('y2','$y2',$str4);
			$data['purchase_price']= eval(" return $str4;");
		} 
		$data['total_price']=$data['purchase_price']*$data['quantity'];
		$data['total_cost']=$cost_price*$data['quantity'];
		$alsobuy = $data['quantity']- $result['cumulative_quantity'];

		if($result['cumulative_quantity']>=$result['quantity']){	//如果累计采购数量大于或等于需要购买的数量
			//查询退货列表是否有退货信息

			$check = M('sales_return')->where(array('order_number'=>$result['order_number'],'sku'=>$result['sku']))->find();
			//需要退货的数量是已经累计购买的数量-修改后需要购买的数量
			$sales_quantity = $result['cumulative_quantity']+$result['purchase_quantity']-$data['quantity'];
			
			if($check){
				$add_sales = M('sales_return')->where(array('id'=>$check['id']))->save(array('supplier'=>$result['supplier'],'buyer'=>$result['buyer'],'salesman'=>$result['salesman'],'sku'=>$result['sku'],'order_number'=>$result['order_number'],'info_id'=>$result['id'],'quantity'=>$sales_quantity));
			}else{
				$add_sales = M('sales_return')->add(array('supplier'=>$result['supplier'],'buyer'=>$result['buyer'],'salesman'=>$result['salesman'],'sku'=>$result['sku'],'order_number'=>$result['order_number'],'info_id'=>$result['id'],'quantity'=>$sales_quantity));
			}
			$wheres = " order_number = '".$data['order_number']."' and sku='".$sku."' and (status='2' or status='3')";
			$result = M('order_info')->where($wheres)->save(array('quantity'=>$data['quantity'],'alsobuy'=>$alsobuy,'total_price'=>$data['total_price'],'total_cost'=>$data['total_cost'],'purchase_price'=>$data['purchase_price']));
			// $result = M('order_info')->where(array('order_number'=>$data['order_number'],'sku'=>$sku,'status'=>'2'))->save(array('quantity'=>$data['quantity'],'alsobuy'=>$alsobuy,'total_price'=>$data['total_price'],'total_cost'=>$data['total_cost'],'purchase_price'=>$data['purchase_price']));
		}else{
			$check = M('sales_return')->where(array('order_number'=>$result['order_number'],'sku'=>$result['sku']))->find();
			
			if($result['alsobuy']>$check['quantity']){
				$dele = M('sales_return')->where(array('id'=>$check['id']))->delete();
			}else{
				$sales_quantity = $result['alsobuy']-$check['quantity'];
				$add_sales = M('sales_return')->where(array('id'=>$check['id']))->save(array('supplier'=>$result['supplier'],'buyer'=>$result['buyer'],'salesman'=>$result['salesman'],'sku'=>$result['sku'],'order_number'=>$result['order_number'],'info_id'=>$result['id'],'quantity'=>$sales_quantity));
			}
			$wheres = " order_number = '".$data['order_number']."' and sku='".$sku."' and (status='2' or status='3')";
			$result = M('order_info')->where($wheres)->save(array('quantity'=>$data['quantity'],'alsobuy'=>$alsobuy,'total_price'=>$data['total_price'],'total_cost'=>$data['total_cost'],'purchase_price'=>$data['purchase_price']));
			// $result = M('order_info')->where(array('order_number'=>$data['order_number'],'sku'=>$sku,'status'=>'2'))->save(array('quantity'=>$data['quantity'],'alsobuy'=>$alsobuy,'total_price'=>$data['total_price'],'total_cost'=>$data['total_cost'],'purchase_price'=>$data['purchase_price']));
		}
			
		return $res;
	}
	
	
	//新增订单明细表
	public function details_info($order_number){
		$order = M("order_info");
		$where['order_number'] = $order_number;
		$where['sales_return']="";
		$res = $order->where($where)->select();
		return $res;
		
	}
	
	
}


?>